<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Config\Advertisement\CreateRequest;
use App\Http\Requests\Config\Advertisement\UpdateRequest;
use App\Models\Advertisement;
use App\Traits\WithFile;
use App\Http\Controllers\Controller;

class AdvertisementController extends Controller
{

    use WithFile;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('广告配置')) {
            return view('error.error');
        }
        $advertisements = Advertisement::with(['author'])->paginate(15);
        return view('advertisement.list', ['advertisements' => $advertisements]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('广告配置')) {
            return view('error.error');
        }
        return view('advertisement.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CreateRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Throwable
     */
    public function store(CreateRequest $request)
    {
        $model = \DB::transaction(function () use ($request) {
            $keys = array_keys($request->validated());
            $advertisement = new Advertisement();
            $advertisement = $this->setModel($advertisement)->upload($keys, $request, 'public');
            return $advertisement;
        });
        return redirect()->route('advertisement.index')->with('success', '添加成功');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(advertisement $advertisement)
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('广告配置')) {
            return view('error.error');
        }
        return view('advertisement.create', ['advertisement' => $advertisement]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateRequest $request
     * @param advertisement $advertisement
     * @return \Illuminate\Http\Response
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, Advertisement $advertisement)
    {
        $model = \DB::transaction(function () use ($request, $advertisement) {
            $keys = array_keys($request->validated());
            $video = $this->setModel($advertisement)->upload($keys, $request, 'public');
            return $video;
        });
        return redirect()->route('advertisement.index')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Advertisement::delete($id);
        return redirect()->route('advertisement.index')->with('success', '删除成功');
    }
}
